<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;

class ChangeCarLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $changeCarModel;

    public function _initialize()
    {
        $this->changeCarModel = D('ChangeCar', 'Model');
    }

    /**
     *  换车信息列表
     *
     * @param int $page
     * @param int $size
     * @param     $params
     */
    public function getChangeCarList($page = 1, $size = 10, $params)
    {
        $map = [];
        $map['chg_status'] = ['eq', $params['status']];
        $map['status'] = ['eq', 1];
        if ($params['status'] == 1) {
            if ($params['start_time']) {
                $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
            }
            if ($params['end_time']) {
                $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['create_time'] = [
                    ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                    ['elt', strtotime($params['end_time'] . ' 23:59:59')]
                ];
            }
            $order = 'id ASC';
        } else {
            if ($params['start_time']) {
                $map['deal_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
            }
            if ($params['end_time']) {
                $map['deal_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
            }
            if ($params['start_time'] && $params['end_time']) {
                $map['deal_time'] = [
                    ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                    ['elt', strtotime($params['end_time'] . ' 23:59:59')]
                ];
            }
            $order = 'deal_time DESC';
        }
        if ($params['location_area'] > 0) {
            $map['cur_location'] = ['eq', $params['location_area']];
        }
        $cols = 'id as chg_id,uid,chg_no,chg_status,status,cur_car_series,cur_reg_time,
                 cur_mileage,cur_location,chg_new_and_old_type,chg_car_type,chg_car_brand,
                 chg_car_age,chg_budget,mobile,come_from,emp_id,deal_time,create_time';
        $total = $this->changeCarModel->field($cols)
            ->where($map)->count();
        $list = $this->changeCarModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
//        echo $this->changeCarModel->_sql();
        if (!empty($list)) {
            $addr = new Address();
            $cm = new CarModel();
            foreach ($list as $k => $v) {
//                $list[$k]['chg_status_name'] = C('CHANGE_CAR_STATUS')[$v['chg_status']];
                $curSeries = $cm->get_model_by_id($v['cur_car_series'], 'series');
                $curBrand = $cm->get_model_by_id($curSeries['brand_id'], 'brand');
                $list[$k]['cur_car_series'] = $curBrand['name'] . ' ' . $curSeries['name'];
                $list[$k]['cur_reg_time'] = date('Y年m月', strtotime($v['cur_reg_time'])) . '初登';
                $list[$k]['cur_mileage'] = $v['cur_mileage'] . '万公里';
                $list[$k]['cur_location'] = $addr->get_name_by_code($v['cur_location']);
                $list[$k]['chg_new_and_old_type'] = C('CHANGE_CAR_NEW_AND_OLD_TYPE')[$v['chg_new_and_old_type']];
                $list[$k]['chg_car_type'] = C('CHANGE_CAR_TYPE')[$v['chg_car_type']];
                $chgCarBrandArr = explode(',', $v['chg_car_brand']);
                $brandName = [];
                foreach ($chgCarBrandArr as $kc => $vc) {
                    $series = $cm->get_model_by_id($vc, 'series');
                    $brand = $cm->get_model_by_id($series['brand_id'], 'brand');
                    $brandName[$kc] = $brand['name'] . '-' . $series['name'];
                }
                $list[$k]['chg_car_brand'] = implode(' ', $brandName);
                $list[$k]['chg_car_age'] = C('CHANGE_CAR_AGE')[$v['chg_car_age']];
                $list[$k]['chg_budget'] = C('CHANGE_CAR_BUDGET')[$v['chg_budget']];
                $list[$k]['come_from'] = C('CHANGE_CAR_COME_FROM')[$v['come_from']];
                $list[$k]['deal_time'] = date("Y.m.d H:i", $v['deal_time']);
                $list[$k]['create_time'] = date("Y.m.d H:i", $v['create_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }


    /**
     * 换车信息详情
     *
     * @param $chgId
     */
    public function getChangeCarInfo($chgId)
    {
        if (empty($chgId)) {
            return false;
        }
        $condition['id'] = ['eq', $chgId];
        $cols = 'id as chg_id,chg_no,chg_status,cur_car_series,cur_reg_time,cur_mileage,
                 cur_location,chg_new_and_old_type,chg_car_type,chg_car_brand,chg_car_age,
                 chg_budget,mobile,remark,create_time';
        $res = $this->changeCarModel->field($cols)->where($condition)->find();
        $resInfo['chg_id'] = $res['chg_id'];
        $resInfo['chg_no'] = $res['chg_no'];
        $resInfo['chg_status'] = $res['chg_status'];
        $addr = new Address();
        $cm = new CarModel();
        $resInfo['create_time'] = date("Y-m-d", strtotime($res['create_time']));
        $curSeries = $cm->get_model_by_id($res['cur_car_series'], 'series');
        $curBrand = $cm->get_model_by_id($curSeries['brand_id'], 'brand');
        $resInfo['cur_car_series'] = $curBrand['name'] . ' ' . $curSeries['name'];
        $resInfo['cur_reg_time'] = date('Y-m', strtotime($res['cur_reg_time']));
        $resInfo['cur_mileage'] = $res['cur_mileage'] . '万公里';
        $resInfo['cur_loacation'] = $addr->get_name_by_code($res['cur_location']);
        $resInfo['new_and_old_type'] = C('CHANGE_CAR_NEW_AND_OLD_TYPE')[$res['chg_new_and_old_type']];
        $resInfo['chg_car_type'] = C('CHANGE_CAR_TYPE')[$res['chg_car_type']];
        $chgCarBrandArr = explode(',', $res['chg_car_brand']);
        $brandName = [];
        foreach ($chgCarBrandArr as $kc => $vc) {
            $series = $cm->get_model_by_id($vc, 'series');
            $brand = $cm->get_model_by_id($series['brand_id'], 'brand');
            $brandName[$kc] = $brand['name'] . '-' . $series['name'];
        }
        $resInfo['chg_car_brand'] = implode('、', $brandName);
        $resInfo['chg_budget'] = C('CHANGE_CAR_BUDGET')[$res['chg_budget']];
        $resInfo['chg_car_age'] = C('CHANGE_CAR_AGE')[$res['chg_car_age']];
        $resInfo['mobile'] = $res['mobile'];
        $resInfo['remark'] = $res['remark'];
        return $resInfo;
    }

    /**
     * 更新换车信息
     *
     * @param array $data
     */
    public function updateChangeCar($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['update_time'] = time();
        return $this->changeCarModel->save($data);
    }

    public function getChangeCarByMobile()
    {
        if (empty($mobile)) {
            return false;
        }
        $condition['status'] = ['eq', 1];
        $condition['mobile'] = ['eq', $mobile];
        return $this->changeCarModel->where($condition)->order('id DESC')->find();
    }

    /**
     * 更新单条置换信息
     *
     * @param array $changeCarData
     *
     * @return int
     */
    public function saveChangeCarInfo($changeCarData = [])
    {
        if (empty($changeCarData)) {
            return false;
        }
        $changeCarData['update_time'] = time();
        return $this->changeCarModel->save($changeCarData);
    }
}